const router = require("koa-router")({
  prefix: "/api/comment"
});
const cartModel = require("../../db/models")["cart"];
const commentModel = require("../../db/models")["comment"];
const { Op } = require("sequelize");
// 获取单个商品的所有评论
router.get("/:id", async ctx => {
  const id = ctx.params.id;
  const comments = await commentModel.findAll({
    where: {
      bid: id
    }
  });
  ctx.body = {
    code: 200,
    data: comments
  };
});

router.post("/:id", async ctx => {
  const id = ctx.params.id;
  const uid = ctx.$$user.id;
  const content = ctx.request.body.comment;
  const comment = await commentModel.create({
    uid,
    bid: id,
    content,
    username: ctx.$$user.username
  });
  ctx.body = {
    code: 200,
    data: comment
  };
});
module.exports = router;
